package com.webmagic.webmagic.service.impl;

import com.alibaba.fastjson.JSON;
import com.webmagic.webmagic.Scheduler.MyRedisScheduler;
import com.webmagic.webmagic.processor.CsdnPageProcessor;
import com.webmagic.webmagic.service.RestTemplateService;
import com.webmagic.webmagic.utils.IOUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Spider;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author xbl
 * @Description
 * @date 2019/1/4
 */
@Service
@Slf4j
public class RestTemplateServiceImpl implements RestTemplateService {

    @Autowired
    private RestTemplate restTemplate;

    private static Integer num = 0;

    @Override
    public String test(String url) {
        String forObject = restTemplate.getForObject(url, String.class);
        String s = JSON.toJSONString(forObject);
        return s;
    }

    @Override
    public void picture(HttpServletResponse response, String url) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        num++;
        log.error("现在为第" + num + "次");
        try {
            URL imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imgUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            inputStream = conn.getInputStream();
            outputStream = response.getOutputStream();
            IOUtil.copyInToOut(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start() {
        MyRedisScheduler myRedisScheduler = new MyRedisScheduler("127.0.0.1");
        myRedisScheduler.setFirst(false);
        Spider.create(new CsdnPageProcessor()).addUrl("https://hacpai.com/tag/Java").setScheduler(myRedisScheduler).thread(5).start();
    }
}
